<?php

include 'LoggerStub.php';

include 'eu/freeplace/validator/Validator.php';

use eu\freeplace\validator\Validator as Validator;

/**
 * @author Elmar Dott
 */
class ValidatorTest extends \PHPUnit_Framework_TestCase {

	
    public function testMakeSecure() {
        $validator = Validator::getInstance();

        $this->assertEquals('1s45e_wä@', $validator->makeSecure('1s45e_wä@'));
        //remove < >
        $this->assertEquals('s45e_wä@', $validator->makeSecure('<h1>s45e_wä@</h1>'));
        $this->assertEquals('', $validator->makeSecure('<s45e_wä@>'));
        //remove \
        $this->assertEquals('A1', $validator->makeSecure('\A1'));
        $this->assertEquals('A1', $validator->makeSecure('\A\\\1\\'));
    }
    
    public function testBooleanValidation() {

        $validator = Validator::getInstance();

        //POSITIVE EXAMPLES
        $this->assertEquals(1, $validator->validate(Validator::BOOLEAN, 1));
        $this->assertEquals(0, $validator->validate(Validator::BOOLEAN, 0));

        $this->assertEquals('true', $validator->validate(Validator::BOOLEAN, 'true'));
        $this->assertEquals('false', $validator->validate(Validator::BOOLEAN, 'false'));

        $this->assertEquals('TRUE', $validator->validate(Validator::BOOLEAN, 'TRUE'));
        $this->assertEquals('FALSE', $validator->validate(Validator::BOOLEAN, 'FALSE'));

        //NEGATIVE EXAMPLES
        $this->assertEquals(0, $validator->validate(Validator::BOOLEAN, 'X'));
        $this->assertEquals(0, $validator->validate(Validator::BOOLEAN, '2'));
    }

    public function testNumberValidation() {

        $validator = Validator::getInstance();

        //POSITIVE EXAMPLES
        $this->assertEquals(0, $validator->validate(Validator::NUMBER, 0));
        $this->assertEquals(-0, $validator->validate(Validator::NUMBER, -0));
        $this->assertEquals(1, $validator->validate(Validator::NUMBER, 1));
        $this->assertEquals(-1, $validator->validate(Validator::NUMBER, -1));
        $this->assertEquals(-147, $validator->validate(Validator::NUMBER, -147));
        $this->assertEquals(0123456789, $validator->validate(Validator::NUMBER, 0123456789));

        //NEGATIVE EXAMPLES
        $this->assertEquals(0, $validator->validate(Validator::NUMBER, "a")); 
        $this->assertEquals(0, $validator->validate(Validator::NUMBER, "011-")); 
        $this->assertEquals(0, $validator->validate(Validator::NUMBER, "+1a")); 
        $this->assertEquals(0, $validator->validate(Validator::NUMBER, "1a0")); 
        
    }

    public function testIsNoEmptyStrings() {
        $validator = Validator::getInstance();

        //POSITIVE EXAMPLES
        $this->assertEquals(0,     $validator->isNotEmpty(0) );
        $this->assertEquals(42,    $validator->isNotEmpty(42) );
        $this->assertEquals("Zä",  $validator->isNotEmpty("Zä") );
        $this->assertEquals(" ah? ", $validator->isNotEmpty(" ah? ") );
        $this->assertEquals("ah? 2", $validator->isNotEmpty("ah? 2") );
        
        //NEGATIVE EXAMPLES
        $empty = "";
        $this->assertEquals(null, $validator->isNotEmpty($empty)); 
        //TODO BUGFIX THE IMPLEMENTATION
#        $this->assertEquals(null, $validator->isNotEmpty("  ")); 
    }
    
}//CLASS

?>